package org.budo.graph.service.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.budo.graph.Constant;
import org.budo.support.freemarker.FreemarkerUtil;
import org.budo.support.lang.util.MapUtil;
import org.budo.support.spring.io.util.ResourceUtil;
import org.springframework.context.ApplicationContext;

import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.alibaba.druid.stat.DruidStatService;
import com.alibaba.druid.support.http.stat.WebAppStatManager;

/**
 * @author limingwei
 */
public class MethodRequestUtil {
    public static String writeMethodRequestResponse(ApplicationContext applicationContext, String url) {
        Map<String, String> parameters = DruidStatService.getParameters(url);
        String method = parameters.get("method");

        if (null == method) {
            return "METHOD_IS_NULL";
        }

        DruidStatManagerFacade druidStatManager = DruidStatManagerFacade.getInstance();
        if (null == druidStatManager) {
            return "DRUID_STAT_MANAGER_FACADE_INSTANCE_IS_NULL";
        }

        List<Map<String, Object>> uriStatList = listByMethod(method);

        Map<String, Object> map = MapUtil.stringObjectMap("uriStatList", uriStatList, "method", method);
        String inputString = ResourceUtil.classPathResourceInputStreamToString(Constant.GRAPH_TEMPLATE_PATH + "/method-request.htm");

        return FreemarkerUtil.freemarkerRender(inputString, map);
    }

    private static List<Map<String, Object>> listByMethod(String method) {
        List<Map<String, Object>> listBySql = new ArrayList<Map<String, Object>>();

        List<Map<String, Object>> uriStatList = WebAppStatManager.getInstance().getURIStatData();

        for (Map<String, Object> uriStat : uriStatList) {
            if (matchMethod(uriStat, method)) {
                listBySql.add(uriStat);
            }
        }

        return listBySql;
    }

    private static boolean matchMethod(Map<String, Object> uriStat, String method) {
        List profiles = (List) uriStat.get("Profiles");

        for (Object profile : profiles) {
            Map profileMap = (Map) profile;

            Object profileType = profileMap.get("Type");
            if (!"SPRING".equals(profileType)) {
                continue;
            }

            String method_1 = method.trim();
            String method_2 = method_1.toUpperCase();

            String name_1 = (String) profileMap.get("Name");
            String name_2 = name_1.trim();
            String name_3 = name_2.toUpperCase();

            if (method_2.endsWith(name_3)) {
                return true;
            }

            String parent_1 = (String) profileMap.get("Parent");
            String parent_2 = parent_1.trim();
            String parent_3 = parent_2.toUpperCase();
            if (method_2.endsWith(parent_3)) {
                return true;
            }
        }

        return false;
    }
}